//
//  GoodsFromMenuView.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/17.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "GoodsFromMenuView.h"
#import "GoodsListCell.h"
#import "ProductModel.h"
#import "MJRefresh.h"
#import "PdcDetailVC.h"
//所有商品URL
#define GOODSLIST_URL @"/api/ApiGoods/getGoodsList"

@interface GoodsFromMenuView ()<UITableViewDataSource,UITableViewDelegate>
{
    //当前页数
    int _currentPage;
    //数据源
    NSMutableArray *_data;
    //列表
    UITableView *_tableView;
    //结果统计
    UILabel *_resultLab;
    //记录个数
    NSString *_count;
    //首次加载标志
    BOOL _isFirstFlag;
}

@end
@implementation GoodsFromMenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
       self.seach_text = @"";
        self.classic_id = @"";
        _count = @"";
        _isFirstFlag = YES;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _currentPage = 1;
        _data = [NSMutableArray array];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"GoodsListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableView.rowHeight = 105.0f;
        
//        _resultLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 35)];
//        _resultLab.textAlignment = NSTextAlignmentLeft;
//        _resultLab.backgroundColor = [UIColor groupTableViewBackgroundColor];
//        _resultLab.font = [UIFont systemFontOfSize:14];
//        if (self.seach_word == nil) {
//            self.seach_word = @"";
//        }
//        _resultLab.text = [NSString stringWithFormat:@"    %@%@",self.seach_word,_count];
//        _tableView.tableHeaderView = _resultLab;
        [self addSubview:_tableView];
        [self setupRefresh];
    }
    return self;
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    //    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // dateKey用于存储刷新时间，可以保证不同界面拥有不同的刷新时间
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    _currentPage = 1;
    [_data removeAllObjects];
    [self startRequestForData];
}

- (void)footerRereshing
{
    _currentPage++;
    [self startRequestForData];
}

//分类刷新
- (void)reloadDataWithClassicId:(NSString *)classic_id
{
    _seach_text = @"";
    _classic_id = classic_id;
    [_data removeAllObjects];
    [self startRequestForData];
}
//搜索字刷新
- (void)reloadDataWithSeachText:(NSString *)text
{
    _seach_text = text;
    _classic_id = @"";
    [_data removeAllObjects];
    [self startRequestForData];
}
//请求网络数据
- (void)startRequestForData
{
    if (_isFirstFlag) {
        [WYPublic addHudToWindow];
        _isFirstFlag = NO;
    }
    NSDictionary *params = @{@"cateid":self.classic_id,@"filter":@"20",@"num":@"20",@"page":[NSNumber numberWithInt:_currentPage],@"search":self.seach_text};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,GOODSLIST_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSDictionary *ddc = dic[@"data"];
        _count = ddc[@"total"];
        NSArray *pdcs = ddc[@"list"];
        for (NSDictionary *pdc in pdcs) {
            ProductModel *model = [[ProductModel alloc]init];
            model.pdc_id = pdc[@"id"];
            model.pdc_sid = pdc[@"sid"];
            model.pdc_qishu = pdc[@"qishu"];
            model.pdc_title = pdc[@"title"];
            model.pdc_sub_title = pdc[@"title2"];
            model.pdc_desc = pdc[@"description"];
            model.pdc_people_indeed = pdc[@"zongrenshu"];
            model.pdc_people_now = pdc[@"canyurenshu"];
            model.pdc_people_left = pdc[@"shenyurenshu"];
            model.pdc_img_path = pdc[@"thumb_url"];
            model.is_locked = pdc[@"is_locked"];
            [_data addObject:model];
        }
        _resultLab.text = [NSString stringWithFormat:@"    %@(%@)",self.seach_word,_count];
        [_tableView reloadData];
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
        [WYPublic removeHudToWindow];
    } failedBlock:^{
        [WYPublic removeHudToWindow];
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
    } needLog:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (_data.count>0) {
        //获取当前数据模型
        ProductModel *model = _data[indexPath.row];
        //填充cell
        [cell fillCellWithModel:model];
        cell.tableView = _tableView;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //保存至兴趣记录
    ProductModel *currentModel = _data[indexPath.row];
    WYShopCarModel *model = [[WYShopCarModel alloc]init];
    model.car_id = currentModel.pdc_id;
    model.car_new_id = currentModel.pdc_sid;
    model.name = currentModel.pdc_title;
    model.imgPath = currentModel.pdc_img_path;
    model.count = 1;
    [[WYDataBase sharedDataBase]insertPdcToIntrestCarWithModel:model];
    //改变是否跳往商品详情的标记
    self.father.isGotoDetailFlag = YES;
    //跳往商品详情
    PdcDetailVC *detailVC = [[PdcDetailVC alloc]init];
    detailVC.pdc_id = currentModel.pdc_id;
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:detailVC animated:YES];
}
@end
